package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwacT;
import cn.e4j.modular.crm.entity.GwaclT;
import cn.e4j.modular.crm.mapper.GwacTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 交易币别 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-11
 */
@Service
public class Gwai210ServiceImpl extends ServiceImpl<GwacTMapper, GwacT>{

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GwacT gwacT) {
        QueryWrapper<GwacT> gwacTQueryWrapper = new QueryWrapper<>();
        gwacTQueryWrapper.eq("GWACENT",gwacT.getGWACENT());
        gwacTQueryWrapper.eq("GWAC001",gwacT.getGWAC001());
        return this.baseMapper.selectList(gwacTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GwacT gwacT, GwaclT gwaclT){
        gwacT.insert();
        gwaclT.insert();
    }

    @Transactional
    public void editData(GwacT gwacT, GwaclT gwaclT,GwacT oldGwacT){
        //修改1
        UpdateWrapper<GwacT> gwacTUpdateWrapper = new UpdateWrapper<>();
        gwacTUpdateWrapper.eq("GWACENT",oldGwacT.getGWACENT());
        gwacTUpdateWrapper.eq("GWAC001",oldGwacT.getGWAC001());
        //修改2
        UpdateWrapper<GwaclT> gwaclTUpdateWrapper = new UpdateWrapper<>();
        gwaclTUpdateWrapper.eq("GWACLENT",oldGwacT.getGWACENT());
        gwaclTUpdateWrapper.eq("GWACL001",oldGwacT.getGWAC001());
        gwaclTUpdateWrapper.eq("GWACL002",gwaclT.getGWACL002());

        gwacT.update(gwacTUpdateWrapper);
        gwaclT.update(gwaclTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwacT gwacT, GwaclT gwaclT){
        delete(gwacT,gwaclT);
    }

    @Transactional
    public void deleteDataBatch(List<GwacT> gwacTList,List<GwaclT> gwaclTList){
        for(int i=0; i < gwacTList.size(); i++){
            delete(gwacTList.get(i),gwaclTList.get(i));
        }
    }

    private void delete(GwacT gwacT, GwaclT gwaclT){
        //修改1
        UpdateWrapper<GwacT> gwacTUpdateWrapper = new UpdateWrapper<>();
        gwacTUpdateWrapper.eq("GWACENT",gwacT.getGWACENT());
        gwacTUpdateWrapper.eq("GWAC001",gwacT.getGWAC001());
        //修改2
        UpdateWrapper<GwaclT> gwaclTUpdateWrapper = new UpdateWrapper<>();
        gwaclTUpdateWrapper.eq("GWACLENT",gwaclT.getGWACLENT());
        gwaclTUpdateWrapper.eq("GWACL001",gwaclT.getGWACL001());
        gwaclTUpdateWrapper.eq("GWACL002",gwaclT.getGWACL002());

        gwacT.delete(gwacTUpdateWrapper);
        gwaclT.delete(gwaclTUpdateWrapper);
    }


}
